package com.xr.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xr.sunnyday.constant.MessageConstant;
import com.xr.sunnyday.entity.PageResult;
import com.xr.sunnyday.entity.QueryPageBean;
import com.xr.sunnyday.entity.Result;
import com.xr.sunnyday.pojo.Setmeal;
import com.xr.sunnyday.service.SetMealService;
import com.xr.sunnyday.utils.QiNiuUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/setMeal")
public class SetMealController {
    @Reference
    private SetMealService setMealService;


    @PostMapping("/findCondition")
    public Result findCondition(@RequestBody QueryPageBean queryPageBean){
         PageResult pageResult= setMealService.findByCondition(queryPageBean);
         return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,pageResult);
    }

    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile){
        String originalFilename = imgFile.getOriginalFilename();
        //获取后缀名
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String filename = UUID.randomUUID() + extension;
        try{
            QiNiuUtils.uploadViaByte(imgFile.getBytes(),filename);
            Map<String,String> map = new HashMap<String,String>();
            map.put("imgName",filename);
            map.put("domain", QiNiuUtils.DOMAIN);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,map);

        } catch (IOException e){
           e.printStackTrace();
        }
        return new Result(true, MessageConstant.PIC_UPLOAD_FAIL);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        setMealService.add(setmeal,checkgroupIds);
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    @PostMapping("/findBySetMealId")
    public Result findBySetMealId(int setMealId){
        Setmeal setmeal= setMealService.findBySetMealId(setMealId);
        Map<String,Object> resultMap = new HashMap<String,Object>();
        //2.1将查询到的对应套餐返回，其中包含了img的图片名称
        resultMap.put("setmeal", setmeal);
        //2.2返回我们七牛云的外连接域名(指向我们的存储空间)
        resultMap.put("domain", QiNiuUtils.DOMAIN);
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,resultMap);
    }

    @PostMapping("/findSetMealCheckGroup")
    public Result findSetMealCheckGroup(int setMealId){
        List<Integer> checkGroupIdList =setMealService.findSetMealCheckGroup(setMealId);
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,checkGroupIdList);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        setMealService.update(setmeal,checkgroupIds);
        return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
    }

    @PostMapping("/delete")
    public Result delete(int id){
        setMealService.delete(id);
        return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
    }

}
